package com.jlrdb.DAO;

import java.util.List;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.EntityNotFoundException;
import com.google.appengine.api.datastore.FetchOptions;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.Query;
import com.google.appengine.api.datastore.Transaction;
import com.google.appengine.api.datastore.Query.SortDirection;
import com.jlrdb.model.AnalogRadioBroadcast;
import com.jlrdb.model.AnalogRadioBroadcasts;
import com.jlrdb.model.DigitalRadioBroadcast;
import com.jlrdb.model.DigitalRadioBroadcasts;

public class RadioBroadcastDAOImpl implements RadioBroadcastDAO {
	@Override
	public int addRadioBroadcastDigital(DigitalRadioBroadcast digitalRadioBroadcast) {
		DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
		Transaction txn = datastore.beginTransaction();
		Entity radiodata = new Entity("DigitalRadioBroadcastData");
		radiodata.setProperty("Country", digitalRadioBroadcast.getCountry());
		radiodata.setProperty("Geographic_Region", digitalRadioBroadcast.getGeographic_Region());
		radiodata.setProperty("Analog_Radio", digitalRadioBroadcast.getAnalog_radio());
		radiodata.setProperty("Digitization_Status", digitalRadioBroadcast.getDigitization_Status());
		radiodata.setProperty("Digital_Radio_Family", digitalRadioBroadcast.getDigital_Radio_Family());
		radiodata.setProperty("Standard", digitalRadioBroadcast.getStandard());
		radiodata.setProperty("Transmission_Status", digitalRadioBroadcast.getTransmission_Status());
		radiodata.setProperty("Coverage", digitalRadioBroadcast.getCoverage());
		radiodata.setProperty("Start_Date", digitalRadioBroadcast.getStart_Date());
		radiodata.setProperty("Stop_date", digitalRadioBroadcast.getStop_date());
		radiodata.setProperty("Notes", digitalRadioBroadcast.getNotes());
		radiodata.setProperty("Frequency_Spectrum", digitalRadioBroadcast.getFrequency_Spectrum());
		radiodata.setProperty("Major_Players", digitalRadioBroadcast.getMajor_Players());
		radiodata.setProperty("Digital_Radio_Overall_coverage", digitalRadioBroadcast.getDigital_Radio_Overall_coverage());
		radiodata.setProperty("MultiPlex", digitalRadioBroadcast.getMultiPlex());
		radiodata.setProperty("ASO_Start_Date", digitalRadioBroadcast.getASO_Start_Date());
		radiodata.setProperty("ASO_End_Date", digitalRadioBroadcast.getASO_End_Date());
		radiodata.setProperty("Regulatory_authority", digitalRadioBroadcast.getRegulatory_authority());
		radiodata.setProperty("Source", digitalRadioBroadcast.getSource());
		radiodata.setProperty("Last_Update_Date", digitalRadioBroadcast.getLast_Update_Date());
		datastore.put(radiodata);
		txn.commit();
		return 0;
	}

	@Override
	public DigitalRadioBroadcasts viewRadioBroadcastsDigital(String criteria, String value) {
		DigitalRadioBroadcasts digitalRadioBroadcasts = new DigitalRadioBroadcasts();
		DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
		Query digitalRadioBroadcastData = null;
		if (criteria.equals("country"))
		{
			digitalRadioBroadcastData = new Query("DigitalRadioBroadcastData").addSort("Country", SortDirection.ASCENDING).addFilter("Country", Query.FilterOperator.EQUAL, value);
		} else if (criteria.equals("geographic_region"))
		{
			digitalRadioBroadcastData = new Query("DigitalRadioBroadcastData").addSort("Country", SortDirection.ASCENDING).addFilter("Geographic_Region", Query.FilterOperator.EQUAL, value);
		}
		List<Entity> results = datastore.prepare(digitalRadioBroadcastData).asList(FetchOptions.Builder.withDefaults());
		if (!results.isEmpty())
		{
			for (Entity result : results)
			{
				DigitalRadioBroadcast radiodata = new DigitalRadioBroadcast();
				radiodata.setCountry(result.getProperty("Country").toString());
				radiodata.setGeographic_Region(result.getProperty("Geographic_Region").toString());
				radiodata.setAnalog_radio(result.getProperty("Analog_Radio").toString());
				radiodata.setDigitization_Status(result.getProperty("Digitization_Status").toString());
				radiodata.setDigital_Radio_Family(result.getProperty("Digital_Radio_Family").toString());
				radiodata.setStandard(result.getProperty("Standard").toString());
				radiodata.setTransmission_Status(result.getProperty("Transmission_Status").toString());
				radiodata.setCoverage(result.getProperty("Coverage").toString());
				radiodata.setStart_Date(result.getProperty("Start_Date").toString());
				radiodata.setStop_date(result.getProperty("Stop_date").toString());
				radiodata.setNotes(result.getProperty("Notes").toString());
				radiodata.setFrequency_Spectrum(result.getProperty("Frequency_Spectrum").toString());
				radiodata.setMajor_Players(result.getProperty("Major_Players").toString());
				radiodata.setDigital_Radio_Overall_coverage(result.getProperty("Digital_Radio_Overall_coverage").toString());
				radiodata.setMultiPlex(result.getProperty("MultiPlex").toString());
				radiodata.setASO_Start_Date(result.getProperty("ASO_Start_Date").toString());
				radiodata.setASO_End_Date(result.getProperty("ASO_End_Date").toString());
				radiodata.setRegulatory_authority(result.getProperty("Regulatory_authority").toString());
				radiodata.setSource(result.getProperty("Source").toString());
				radiodata.setLast_Update_Date(result.getProperty("Last_Update_Date").toString());
				radiodata.setBroadcast_Id(result.getKey().getId());
				digitalRadioBroadcasts.addRadioBroadcasts(radiodata);
			}
		}
		return digitalRadioBroadcasts;
	}

	@Override
	public DigitalRadioBroadcast getThisBroadcastDigital(long broadcastId) {
		DigitalRadioBroadcast radiodata = new DigitalRadioBroadcast();
		DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
		Key key = KeyFactory.createKey("DigitalRadioBroadcastData", broadcastId);
		Entity result;
		try
		{
			result = datastore.get(key);
			radiodata.setCountry(result.getProperty("Country").toString());
			radiodata.setGeographic_Region(result.getProperty("Geographic_Region").toString());
			radiodata.setAnalog_radio(result.getProperty("Analog_Radio").toString());
			radiodata.setDigitization_Status(result.getProperty("Digitization_Status").toString());
			radiodata.setDigital_Radio_Family(result.getProperty("Digital_Radio_Family").toString());
			radiodata.setStandard(result.getProperty("Standard").toString());
			radiodata.setTransmission_Status(result.getProperty("Transmission_Status").toString());
			radiodata.setCoverage(result.getProperty("Coverage").toString());
			radiodata.setStart_Date(result.getProperty("Start_Date").toString());
			radiodata.setStop_date(result.getProperty("Stop_date").toString());
			radiodata.setNotes(result.getProperty("Notes").toString());
			radiodata.setFrequency_Spectrum(result.getProperty("Frequency_Spectrum").toString());
			radiodata.setMajor_Players(result.getProperty("Major_Players").toString());
			radiodata.setDigital_Radio_Overall_coverage(result.getProperty("Digital_Radio_Overall_coverage").toString());
			radiodata.setMultiPlex(result.getProperty("MultiPlex").toString());
			radiodata.setASO_Start_Date(result.getProperty("ASO_Start_Date").toString());
			radiodata.setASO_End_Date(result.getProperty("ASO_End_Date").toString());
			radiodata.setRegulatory_authority(result.getProperty("Regulatory_authority").toString());
			radiodata.setSource(result.getProperty("Source").toString());
			radiodata.setLast_Update_Date(result.getProperty("Last_Update_Date").toString());
			radiodata.setBroadcast_Id(result.getKey().getId());
		} catch (EntityNotFoundException e)
		{
			e.printStackTrace();
		}
		return radiodata;
	}

	@Override
	public int updateRadioBroadcastDigital(DigitalRadioBroadcast digitalRadioBroadcast, long broadcastId) {
		DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
		Transaction txn = datastore.beginTransaction();
		Entity radiodata = null;
		Key key = KeyFactory.createKey("DigitalRadioBroadcastData", broadcastId);
		try
		{
			radiodata = datastore.get(key);
		} catch (EntityNotFoundException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		// radiodata.setProperty("Country", digitalRadioBroadcast.getCountry());
		// radiodata.setProperty("Geographic_Region",
		// digitalRadioBroadcast.getGeographic_Region());
		radiodata.setProperty("Analog_Radio", digitalRadioBroadcast.getAnalog_radio());
		radiodata.setProperty("Digitization_Status", digitalRadioBroadcast.getDigitization_Status());
		radiodata.setProperty("Digital_Radio_Family", digitalRadioBroadcast.getDigital_Radio_Family());
		radiodata.setProperty("Standard", digitalRadioBroadcast.getStandard());
		radiodata.setProperty("Transmission_Status", digitalRadioBroadcast.getTransmission_Status());
		radiodata.setProperty("Coverage", digitalRadioBroadcast.getCoverage());
		radiodata.setProperty("Start_Date", digitalRadioBroadcast.getStart_Date());
		radiodata.setProperty("Stop_date", digitalRadioBroadcast.getStop_date());
		radiodata.setProperty("Notes", digitalRadioBroadcast.getNotes());
		radiodata.setProperty("Frequency_Spectrum", digitalRadioBroadcast.getFrequency_Spectrum());
		radiodata.setProperty("Major_Players", digitalRadioBroadcast.getMajor_Players());
		radiodata.setProperty("Digital_Radio_Overall_coverage", digitalRadioBroadcast.getDigital_Radio_Overall_coverage());
		radiodata.setProperty("MultiPlex", digitalRadioBroadcast.getMultiPlex());
		radiodata.setProperty("ASO_Start_Date", digitalRadioBroadcast.getASO_Start_Date());
		radiodata.setProperty("ASO_End_Date", digitalRadioBroadcast.getASO_End_Date());
		radiodata.setProperty("Regulatory_authority", digitalRadioBroadcast.getRegulatory_authority());
		radiodata.setProperty("Source", digitalRadioBroadcast.getSource());
		radiodata.setProperty("Last_Update_Date", digitalRadioBroadcast.getLast_Update_Date());
		datastore.put(radiodata);
		txn.commit();
		return 0;
	}

	@Override
	public int addRadioBroadcastAnalog(AnalogRadioBroadcast analogRadioBroadcast) {
		DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
		Transaction txn = datastore.beginTransaction();
		Entity radiodata = new Entity("AnalogRadioBroadcastData");
		radiodata.setProperty("Country", analogRadioBroadcast.getCountry());
		radiodata.setProperty("Geographic_Region", analogRadioBroadcast.getGeographic_Region());
		radiodata.setProperty("Am", analogRadioBroadcast.getAm());
		radiodata.setProperty("Fm", analogRadioBroadcast.getFm());
		radiodata.setProperty("Major_Players", analogRadioBroadcast.getMajor_Players());
		radiodata.setProperty("Notes_Remarks", analogRadioBroadcast.getNotes_Remarks());
		radiodata.setProperty("Last_Update_Date", analogRadioBroadcast.getLast_Date_Updated());
		datastore.put(radiodata);
		txn.commit();
		return 0;
	}

	@Override
	public AnalogRadioBroadcasts viewRadioBroadcastsAnalog(String criteria, String value) {
		AnalogRadioBroadcasts analogRadioBroadcasts = new AnalogRadioBroadcasts();
		DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
		Query digitalRadioBroadcastData = null;
		if (criteria.equals("country"))
		{
			digitalRadioBroadcastData = new Query("AnalogRadioBroadcastData").addSort("Country", SortDirection.ASCENDING).addFilter("Country", Query.FilterOperator.EQUAL, value);
		} else if (criteria.equals("geographic_region"))
		{
			digitalRadioBroadcastData = new Query("AnalogRadioBroadcastData").addSort("Country", SortDirection.ASCENDING).addFilter("Geographic_Region", Query.FilterOperator.EQUAL, value);
		}
		List<Entity> results = datastore.prepare(digitalRadioBroadcastData).asList(FetchOptions.Builder.withDefaults());
		if (!results.isEmpty())
		{
			for (Entity result : results)
			{
				AnalogRadioBroadcast radiodata = new AnalogRadioBroadcast();
				radiodata.setCountry(result.getProperty("Country").toString());
				radiodata.setGeographic_Region(result.getProperty("Geographic_Region").toString());
				radiodata.setAm(result.getProperty("Am").toString());
				radiodata.setFm(result.getProperty("Fm").toString());
				radiodata.setNotes_Remarks(result.getProperty("Notes_Remarks").toString());
				radiodata.setMajor_Players(result.getProperty("Major_Players").toString());
				radiodata.setLast_Date_Updated(result.getProperty("Last_Update_Date").toString());
				radiodata.setBroadcast_Id(result.getKey().getId());
				analogRadioBroadcasts.addRadioBroadcasts(radiodata);
			}
		}
		return analogRadioBroadcasts;
	}

	@Override
	public AnalogRadioBroadcast getThisBroadcastAnalog(long broadcastId) {
		AnalogRadioBroadcast radiodata = new AnalogRadioBroadcast();
		DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
		Key key = KeyFactory.createKey("AnalogRadioBroadcastData", broadcastId);
		Entity result;
		try
		{
			result = datastore.get(key);
			radiodata.setCountry(result.getProperty("Country").toString());
			radiodata.setGeographic_Region(result.getProperty("Geographic_Region").toString());
			radiodata.setAm(result.getProperty("Am").toString());
			radiodata.setFm(result.getProperty("Fm").toString());
			radiodata.setNotes_Remarks(result.getProperty("Notes_Remarks").toString());
			radiodata.setMajor_Players(result.getProperty("Major_Players").toString());
			radiodata.setLast_Date_Updated(result.getProperty("Last_Update_Date").toString());
			radiodata.setBroadcast_Id(result.getKey().getId());
		} catch (EntityNotFoundException e)
		{
			e.printStackTrace();
		}
		return radiodata;
	}

	@Override
	public int updateRadioBroadcastAnalog(AnalogRadioBroadcast analogRadioBroadcast, long broadcastId) {
		DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
		Transaction txn = datastore.beginTransaction();
		Entity radiodata = null;
		Key key = KeyFactory.createKey("AnalogRadioBroadcastData", broadcastId);
		try
		{
			radiodata = datastore.get(key);
		} catch (EntityNotFoundException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		// radiodata.setProperty("Country", digitalRadioBroadcast.getCountry());
		// radiodata.setProperty("Geographic_Region",
		radiodata.setProperty("Am", analogRadioBroadcast.getAm());
		radiodata.setProperty("Fm", analogRadioBroadcast.getFm());
		radiodata.setProperty("Major_Players", analogRadioBroadcast.getMajor_Players());
		radiodata.setProperty("Notes_Remarks", analogRadioBroadcast.getNotes_Remarks());
		radiodata.setProperty("Last_Update_Date", analogRadioBroadcast.getLast_Date_Updated());
		datastore.put(radiodata);
		txn.commit();
		return 0;
	}
}
